<template>
  <div>
    <el-dialog title="权限配置" :visible.sync="disVisible" @close="closeForm"
               :close-on-click-modal="true">
      <el-input v-model="role.name" :disabled="true"/>

      <el-tree
          ref="permissionTree"
          :data="permission"
          show-checkbox
          node-key="id"
          :default-expanded-keys="[1]"
          :props="defaultProps">
      </el-tree>
      <el-button type="primary" @click="submitForm">确定</el-button>
      <el-button @click="closeForm">取消</el-button>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "AccessAllot",
  props: {
    formVisible: Boolean,
    formRole: Object
  },
  data() {
    return {
      disVisible: this.formVisible,
      role: this.formRole,
      permission: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
    };
  },
  methods: {
    submitForm() {

      let params = {
        permissionIds: this.$refs.permissionTree.getCheckedKeys(),
        roleId: this.role.id
      };
      this.$axios.postForm("/urlPermission/addAuthority", params)
          .then(response => {
            let responseData = response.data;
        if (responseData.code==200){
          this.$message({
            message:'配置成功',
            type:'success',
            duration:2000
          })
          this.disVisible=false;
          this.$emit('roleList');
        }else {
          this.$message({
            message:'',
            type:'error',
            duration:2000
          })
          return;
        }
      })
    },

    closeForm() {
      this.$emit('update:accessFormVisible', false);
    }
  },
  watch: {
    formVisible(val) {
      this.disVisible = val

      //查询所有权限
      this.$axios.get("/urlPermission/selectList").then(response => {
        let responseData = response.data;
        if (responseData.code == 200) {
          this.permission = responseData.data
        }
      })
    },
    formRole(val) {
      this.role = val
      let roleId = this.role.id;

      //查询当前角色绑定的功能
      this.$axios.postForm("/urlPermission/getPermissionIds", {roleId: roleId})
          .then(response => {
            let responseData = response.data;
            if (responseData.code == 200) {
              this.$refs.permissionTree.setCheckedKeys(responseData.data)
            }
          })

    }
  },
  created() {


  }
}
</script>

<style scoped>

</style>